package cn.exrick.xboot.modules.gatestation.controller;

import cn.exrick.xboot.base.XbootBaseController;
import cn.exrick.xboot.common.utils.CustomPageUtil;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.SnowFlakeUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.gatestation.entity.NaturalgasGateStation;
import cn.exrick.xboot.modules.gatestation.service.NaturalgasGateStationService;
import cn.hutool.core.util.StrUtil;
import com.qiniu.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;



/**
 * @author yck
 */
@Slf4j
@RestController
@Api(description = "门站管理接口")
@RequestMapping("/xboot/naturalgasGateStation")
@Transactional
public class NaturalgasGateStationController extends XbootBaseController<NaturalgasGateStation, String> {

    @Autowired
    private NaturalgasGateStationService naturalgasGateStationService;

    @Override
    public NaturalgasGateStationService getService() {
        return naturalgasGateStationService;
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Object> getByCondition(NaturalgasGateStation naturalgasGateStation,
                                                            SearchVo searchVo,
                                                            PageVo pageVo){

        Page<NaturalgasGateStation> page = naturalgasGateStationService.findByCondition(naturalgasGateStation, searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<Object>().setData(CustomPageUtil.getCustomPage(page));
    }
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "多条件分页获取")
    public Result<Object> saveOrUpdate(NaturalgasGateStation naturalgasGateStation){
        String msg="";
        if(StrUtil.isNotBlank(naturalgasGateStation.getId())){
            naturalgasGateStationService.update(naturalgasGateStation);
            msg="修改成功";
        }else{
            naturalgasGateStation.setId(SnowFlakeUtil.getFlowIdInstance().nextId()+"");
            naturalgasGateStationService.save(naturalgasGateStation);
            msg="保存成功";
        }
        return ResultUtil.success(msg);
    }
    //根据id获取数据
    @RequestMapping(value = "/gateStationById", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public  Result<Object> gateStationById(String id){
        NaturalgasGateStation naturalgasGateStation = naturalgasGateStationService.get(id);
        if(naturalgasGateStation.getGateType().equals("1")){
            naturalgasGateStation.setGateTypeName("门站");
        }else{
            naturalgasGateStation.setGateTypeName("分输站");
        }
        return new ResultUtil<Object>().setData(naturalgasGateStation);
    }
}
